package com.rdc.soulmatemusicplayer.service;

import android.app.Service;

import android.content.Intent;


import android.media.MediaPlayer;
import android.net.Uri;
import android.os.Binder;
import android.os.IBinder;
import android.os.RemoteException;
import android.os.SystemClock;
import android.widget.Toast;

import com.rdc.soulmatemusicplayer.application.App;
import com.rdc.soulmatemusicplayer.base.LocalFragment;
import com.rdc.soulmatemusicplayer.util.MusicUtils;

import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @author Soul Mate
 * @file 文件名
 * @brief 播放音乐用的service
 * @date 创建日期  2021-04-19 16:32
 */

public class PlayService extends Service
{

	final static int NOTPLAYING = -1;

	private MediaPlayer mPlayer;

	private int mPlayingPosition; // 当前正在播放的歌曲位置

	// 单线程池
//	private ExecutorService mProgressUpdatedListener = Executors.newSingleThreadExecutor();

	//用于Media同步SeekBar的进度
	public void SynchronousProgress(int progress)
	{
		mPlayer.seekTo( progress);
	}

	public class PlayBinder extends Binder
	{
		public PlayService getService()
		{
			return PlayService.this;
		}
	}

	@Override
	public IBinder onBind(Intent intent) {

		return new PlayBinder();
	}

	@Override
	public void onCreate()
	{
		super.onCreate();

		//获取歌单
		MusicUtils.initMusicList();

		if (MusicUtils.sMusicList.size() <= 0)
		{
			Toast.makeText(getApplicationContext(), "当前手机没有MP3文件", Toast.LENGTH_LONG).show();
		} else
		{
			if (getPlayingPosition() < 0)
			{
				mPlayingPosition = 0;
			}

			//播放路径
			Uri uri = Uri.parse(MusicUtils.sMusicList.get(getPlayingPosition()).getUri());
			mPlayer = MediaPlayer.create(PlayService.this, uri);

		}

	}


	/**
	* MediaPlayer的播放功能
	* @return 当前播放歌曲的位置
	*/
	public int play(int position)
	{

		//自动下一首的回调方法
		MyOnCompletionListener completionListener = new MyOnCompletionListener();
		mPlayer.setOnCompletionListener(completionListener);

		if(MusicUtils.sMusicList.size()<=0){
			Toast.makeText(getApplicationContext(),
					"当前手机没有MP3文件", Toast.LENGTH_LONG).show();
			return NOTPLAYING ;
		}
		if (position < 0)
			position = 0;
		if (position >= MusicUtils.sMusicList.size())
			position = MusicUtils.sMusicList.size() - 1;

		try {
			mPlayer.reset();
			mPlayer.setDataSource(MusicUtils
					.sMusicList.get(position).getUri());
			mPlayer.prepare();

			mPlayer.start();
		} catch (Exception e)
		{
			e.printStackTrace();
		}

		//返回位置
		mPlayingPosition = position;
		return mPlayingPosition;
	}

	/**
	 * 继续播放
	 *
	 * @return 当前播放的位置 默认为0
	 */
	public int resume() {
		if(mPlayer==null){

			return NOTPLAYING;
		}
		if (isPlaying())
			return NOTPLAYING;
		mPlayer.start();
		return mPlayingPosition;
	}

	/**
	 * 暂停播放
	 *
	 * @return 当前播放的位置
	 */
	public int pause() {
		if(MusicUtils.sMusicList.size()==0){

			return NOTPLAYING;
		}
		if (!isPlaying())
			return NOTPLAYING;
		mPlayer.pause();
		return mPlayingPosition;
	}

	/**
	 * 下一曲
	 *
	 * @return 当前播放的位置
	 */
	public int next() {
		if (mPlayingPosition >= MusicUtils.sMusicList.size() - 1) {
			return play(0);
		}
		return play(mPlayingPosition + 1);
	}

	/**
	 * 上一曲
	 *
	 * @return 当前播放的位置
	 */
	public int pre() {
		if (mPlayingPosition <= 0) {
			return play(MusicUtils.sMusicList.size() - 1);
		}
		return play(mPlayingPosition - 1);
	}

	/**
	 * 是否正在播放
	 *
	 * @return
	 */
	public boolean isPlaying() {

		return null != mPlayer && mPlayer.isPlaying();
	}

	/**
	 * 获取正在播放的歌曲在歌曲列表的位置
	 *
	 * @return
	 */
	public int getPlayingPosition() {
		return mPlayingPosition;
	}

	/**
	 * 获取当前正在播放音乐的总时长
	 *
	 * @return
	 */
	public int getDuration() {
		if (!isPlaying())
			return 0;
		return mPlayer.getDuration();
	}


	//获取当前正在播放音乐的总时长
	public int getCurrent(){
		if ( !isPlaying())
			return 0;
		return mPlayer.getCurrentPosition();
	}


	public class MyOnCompletionListener implements MediaPlayer.OnCompletionListener{
		@Override
		public void onCompletion(MediaPlayer mp) {

			Toast.makeText(getApplicationContext(), "自动为您切下一首：" , Toast.LENGTH_SHORT).show();

			next();
		}
	}


	@Override
	public boolean onUnbind(Intent intent)
	{
		return true;
	}

	@Override
	public void onRebind(Intent intent)
	{
		super.onRebind(intent);
	}

	@Override
	public void onDestroy() {
		release();
		super.onDestroy();
	}

	/**
	 * 服务销毁时，释放各种控件
	 */
	private void release() {
		if (mPlayer != null)
			mPlayer.release();
		mPlayer = null;
	}



}

